package com.suray.attendance.contacts.utils;

import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.tea.TeaException;
import com.suray.attendance.common.constant.DingTalkConstant;
import com.suray.attendance.common.dingding.DingTalkClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: jianglinyu
 * @CreateTime: 2023-07-03  09:55
 * @Description: 获取access_token工具类
 * @Version: 1.0
 */
public class AccessTokenUtil {
    private static final Logger bizLogger = LoggerFactory.getLogger(AccessTokenUtil.class);

    public static String getToken(){
        com.aliyun.dingtalkoauth2_1_0.Client client = null;
        try {
            client = DingTalkClientUtils.createOauthClient();
        } catch (Exception e) {
            bizLogger.error("getOauthClient failed", e);
        }
        com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
                .setAppKey(DingTalkConstant.APPKEY)
                .setAppSecret(DingTalkConstant.APPSECRET);
        try {
            GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
            return accessToken.getBody().getAccessToken();
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
                bizLogger.error("getAccessToken failed", err);
                throw new RuntimeException();
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        }
        return null;
    }
}

